//
//  NativeBackNativeChannel.m
//  kq_flutter_widgets
//
//  Created by wtb on 2023/4/7.
//

#import "NativeBackNativeChannel.h"
#import "KqTool.h"

@implementation NativeBackNativeChannel

+(void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar{
    FlutterMethodChannel *channel = [FlutterMethodChannel methodChannelWithName:@"flutter.keqiang.com/navigation" binaryMessenger:[registrar messenger]];
    NativeBackNativeChannel *back = [[NativeBackNativeChannel alloc] init];
    [registrar addMethodCallDelegate:back channel:channel];
}

-(void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result{
    if ([@"close" isEqualToString:call.method]){
        [KqTool goBack];
    }else if([@"exitLogin" isEqualToString:call.method]){
        //退出登录
        [KqTool exitLogin];
    }else if([@"setOrientationMode" isEqualToString:call.method]){
        // 屏幕旋转
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        NSNumber *code = call.arguments[@"orientationMode"];
        [dic setValue:code forKey:@"orient"];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"flutterPageOrientation" object:nil userInfo:dic];
    }else if([@"open" isEqualToString:call.method]){
        NSString *pageName = call.arguments[@"pageName"];
        NSDictionary *params = call.arguments[@"params"];
        if (pageName && [@"BLE" isEqualToString:pageName]) {
            //跳转到蓝牙
            if ([params objectForKey:@"stationCode"]) {
                [KqTool kqPushWithUrl:@"GFBLEViewController" property:@{@"bleId":[params objectForKey:@"stationCode"]}];
            }
        }else if([pageName isEqualToString:@"countryAreaPage"]){
            id areaCallback = ^(NSString * _Nonnull code) {
                if (code) {
                    result(code);
                }
            };
            NSMutableDictionary *data = [NSMutableDictionary dictionary];
            [data setValue:areaCallback forKey:@"areaCallback"];
            [KqTool kqPushWithUrl:@"GFAreaViewController" property:data];
        }else if([pageName isEqualToString:@"shareWX"]){
            Class class = NSClassFromString(@"FlutterCallNativeFuncTool");
            NSObject *call = [[class alloc] init];
            if ([call respondsToSelector:@selector(shareWebToWx:)]) {
                [call performSelector:@selector(shareWebToWx:) withObject:params];
            }
        }else if([pageName isEqualToString:@"shareDD"]){
            Class class = NSClassFromString(@"FlutterCallNativeFuncTool");
            NSObject *call = [[class alloc] init];
            if ([call respondsToSelector:@selector(shareWebToDT:)]) {
                [call performSelector:@selector(shareWebToDT:) withObject:params];
            }
        }else if([pageName isEqualToString:@"CloudSchool"]){
            //云学堂-小诸葛
            [KqTool kqPushWithUrl:@"GFH5ViewController" property:@{@"h5Type":@"1",@"isWebTitle":@1,@"isBehavior":@1,@"key":[params objectForKey:@"key"],@"language":[params objectForKey:@"language"],@"ip":[params objectForKey:@"ip"]}];
        }else if([pageName isEqualToString:@"XzgPrice"]){
            //报价器-小诸葛
//
            NSString *serverIp = [params objectForKey:@"serverIp"];
            if(serverIp && serverIp.length > 8){
                serverIp = [serverIp substringToIndex:serverIp.length - 8];
                serverIp = [NSString stringWithFormat:@"%@%@",serverIp,@"kq_highnet2_static_web/priceTool/index.html#/"];
                [KqTool kqPushWithUrl:@"GFH5ViewController" property:@{@"url":serverIp,@"isWebTitle":@1,@"isBehavior":@1}];
            }
        }else if([pageName isEqualToString:@"message"]){
            //管工厂云消息中心
            [KqTool kqPushWithUrl:@"GFMsgCenterViewController" property:nil];
        }else if([pageName isEqualToString:@"nativeHome"]){
            [KqTool resetRootViewController];
        }else if([pageName isEqualToString:@"SimpleMacFix"]){
            //原生无流程设备维修
            NSMutableDictionary *data = [NSMutableDictionary dictionary];
            [data setValue:[params objectForKey:@"deviceId"] forKey:@"machineId"];
            [data setValue:[params objectForKey:@"deviceName"] forKey:@"machineName"];
            [data setValue:[params objectForKey:@"remark"] forKey:@"memo"];
            [data setValue:[params objectForKey:@"currentFixProgressType"] forKey:@"processType"];
            // 停机记录过来需要设置isGoList == YES
            [data setValue:[NSNumber numberWithBool:YES] forKey:@"isGoList"];
            // 入口要区分扫码，只要不是扫码，工作台和首页不需要区分
            [data setValue:[NSNumber numberWithInt:0] forKey:@"entry"];
            [data setValue:[params objectForKey:@"sysType"] forKey:@"sysType"];
            [KqTool kqPushWithUrl:@"GFNoProgressDetailViewController" property:data];
        }else if([pageName isEqualToString:@"AddMacFix"]){
            //原生新增设备维修
            NSMutableDictionary *data = [NSMutableDictionary dictionary];
            [data setValue:[params objectForKey:@"deviceId"] forKey:@"machineId"];
            [data setValue:[params objectForKey:@"deviceName"] forKey:@"machineName"];
            [data setValue:[params objectForKey:@"remark"] forKey:@"memo"];
            [data setValue:[params objectForKey:@"currentFixProgressType"] forKey:@"processType"];
            
            [data setValue:[params objectForKey:@"sysType"] forKey:@"sysType"];
            // 停机记录过来需要设置isGoList == YES
            [data setValue:[NSNumber numberWithBool:YES] forKey:@"isGoList"];
            // 入口要区分扫码，只要不是扫码，工作台和首页不需要区分
            [data setValue:[NSNumber numberWithInt:0] forKey:@"entry"];
            [KqTool kqPushWithUrl:@"GFApplyNurseViewController" property:data];
        }else if([pageName isEqualToString:@"SimpleMoldFix"]){
            //原生无流程模具维修
            NSMutableDictionary *data = [NSMutableDictionary dictionary];
            [data setValue:[params objectForKey:@"moldId"] forKey:@"moldId"];
            [data setValue:[params objectForKey:@"moldName"] forKey:@"moldName"];
            [data setValue:[params objectForKey:@"remark"] forKey:@"memo"];
            [data setValue:[params objectForKey:@"currentFixProgressType"] forKey:@"processType"];
            [data setValue:[NSNumber numberWithBool:YES] forKey:@"isGoList"];
            
            // 入口要区分扫码，只要不是扫码，工作台和首页不需要区分
            [data setValue:[NSNumber numberWithInt:0] forKey:@"entry"];
            [KqTool kqPushWithUrl:@"GFNoProgressMoldDetailViewController" property:data];
        }else if([pageName isEqualToString:@"AddMoldFix"]){
            //原生新增模具维修
            NSMutableDictionary *data = [NSMutableDictionary dictionary];
            [data setValue:[params objectForKey:@"moldId"] forKey:@"moldId"];
            [data setValue:[params objectForKey:@"moldName"] forKey:@"moldName"];
            [data setValue:[params objectForKey:@"remark"] forKey:@"memo"];
            [data setValue:[params objectForKey:@"currentFixProgressType"] forKey:@"processType"];
            
            // 停机记录过来需要设置isGoList == YES
            [data setValue:[NSNumber numberWithBool:YES] forKey:@"isGoList"];
            // 入口要区分扫码，只要不是扫码，工作台和首页不需要区分
            [data setValue:[NSNumber numberWithInt:0] forKey:@"entry"];
            [KqTool kqPushWithUrl:@"GFApplyMoldNurseViewController" property:data];
        }else if([pageName isEqualToString:@"MoldRecord"]){
            //模具履历
            NSMutableDictionary *data = [NSMutableDictionary dictionary];
            [data setValue:[params objectForKey:@"moldId"] forKey:@"moldId"];
            [KqTool kqPushWithUrl:@"GFMouldRecordViewController" property:data];
        }else if ([pageName isEqualToString:@"TaskDetail"]){
            // 任务单详情
            NSString *systemType = [params objectForKey:@"systemType"];
            NSMutableDictionary *data = [NSMutableDictionary dictionary];
            [data setValue:[params objectForKey:@"taskNo"] forKey:@"taskNo"];
            if([systemType isEqualToString:@"CNC"]){
                [data setValue:[NSNumber numberWithBool:NO] forKey:@"navItemMark"];
                [KqTool kqPushWithUrl:@"CNCWorkTaskDetailViewController" property:data];
            }else if ([systemType isEqualToString:@"ASSEMBLY"]){
                [data setValue:[NSNumber numberWithBool:YES] forKey:@"ifHideRightNav"];
                [KqTool kqPushWithUrl:@"AssemblyTaskDetailsController" property:data];
            }else{
                [data setValue:[NSNumber numberWithBool:NO] forKey:@"navItemMark"];
                [KqTool kqPushWithUrl:@"GFWorkTasksDetailsViewController" property:data];
            }
        }else if([pageName isEqualToString:@"UnQualifiedDetail"]){
            // 不合格品详情
            NSMutableDictionary *data = [NSMutableDictionary dictionary];
            [data setValue:[params objectForKey:@"recordId"] forKey:@"unqualifiedId"];
            NSString *isRevoke = [params objectForKey:@"isRevoke"];
            if([isRevoke isEqualToString:@"1"]){
                //已撤销
                [data setValue:@"5" forKey:@"status"];
            }else{
                //已结束
                [data setValue:@"4" forKey:@"status"];
            }
            if([[params objectForKey:@"onlyRead"] isEqualToString:@"1"]){
                [data setValue:[NSNumber numberWithBool:YES] forKey:@"isOnlyRead"];
            }else{
                [data setValue:[NSNumber numberWithBool:NO] forKey:@"isOnlyRead"];
            }
            [KqTool kqPushWithUrl:@"GFUnqualifiedDetailViewController" property:data];
        }else if([pageName isEqualToString:@"BadDetail"]){
            // 次品详情
            NSMutableDictionary *data = [NSMutableDictionary dictionary];
            [data setValue:[params objectForKey:@"recordId"] forKey:@"rejectsId"];
            [data setValue:[params objectForKey:@"isDeleteAndEdit"] forKey:@"isDeleteAndEdit"];
            NSString *mode = [params objectForKey:@"mode"];
            if([mode isEqualToString:@"0"]){
                [data setValue:[NSNumber numberWithBool:YES] forKey:@"isNumber"];
            }
            [KqTool kqPushWithUrl:@"GFDefectiveDetailViewController" property:data];
        }else if([pageName isEqualToString:@"MoldFixDetail"]){
            // 维修详情
            NSString *createProcessType = [params objectForKey:@"createProcessType"];
            NSString *currentProcessType = [params objectForKey:@"currentProcessType"];
            NSString *recordId = [params objectForKey:@"recordId"];
            
            NSMutableDictionary *data = [NSMutableDictionary dictionary];
            
            [data setValue:currentProcessType forKey:@"processType"];
            [data setValue:[NSNumber numberWithBool:YES] forKey:@"isNoPower"];
            if([createProcessType isEqualToString:@"0"]){
                // 无流程
                [data setValue:@"1" forKey:@"readOnly"];
                [data setValue:[params objectForKey:@"recordId"] forKey:@"recordId"];
                [KqTool kqPushWithUrl:@"GFNoProgressMoldDetailViewController" property:data];
            }else{
                [data setValue:[params objectForKey:@"recordId"] forKey:@"fixRecordId"];
                [KqTool kqPushWithUrl:@"GFNurseMoldDetailViewController" property:data];
            }
        }else if ([pageName isEqualToString:@"PlanDetail"]){
            NSString *planId = [params objectForKey:@"planId"];
            Class class = NSClassFromString(@"GFUserManager");
            id user = [[class alloc] init];
            id currentFactory = [KqTool getPropertyValueWithObj:user key:@"currentFactory"];
            if(currentFactory){
                NSString *systemTypeId = (NSString *)[KqTool getPropertyValueWithObj:currentFactory key:@"systemTypeId"];
                NSMutableDictionary *data = [NSMutableDictionary dictionary];
                if([systemTypeId isEqualToString:@"IMM"]){
                    [data setValue:[NSNumber numberWithBool:YES] forKey:@"isEditItem"];
                    [data setValue:planId forKey:@"planId"];
                    [KqTool kqPushWithUrl:@"GFPlanDetailViewController" property:data];
                }else if ([systemTypeId isEqualToString:@"CNC"]){
                    [data setValue:planId forKey:@"planId"];
                    [KqTool kqPushWithUrl:@"GFCNCPlanDetailViewController" property:data];
                }else if([systemTypeId isEqualToString:@"ASSEMBLY"]){
                    [data setValue:planId forKey:@"planId"];
                    [KqTool kqPushWithUrl:@"GFAssemblyPlanDetailViewController" property:data];
                }
            }
        }
    }
}

@end
